package  translateUi
{
	import com.utils.RegUtil;
	
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import spark.components.Label;
	
	import utils.RegUtil;
	import utils.StringTool;
	
	/**
	 * 语言转换，中文转 其它 繁体、韩文
	 */
	public class LangTranslateUi
	{
		public static var printMsg:Label;
		public static var exportUrl:String;
		
		private var _path:String;
		private var _path2:String;
		private var _cfgStream:FileStream;
		private var _urlLoader:URLLoader;
		private var _timer:Timer;
		private var _className:String;
		
		private static var _sortIndex:int = 0;
		
		public function LangTranslateUi()
		{
			_timer = new Timer(1000);
		}
		public function doLoad(path:String):void
		{
			//"C:\Users\Administrator\Desktop\ani原始\21000\bg_tiles\1.mp"
			
			_path = path;
			_path2 = path.replace(".ui", "-222.ui");
			
			loadData();
		}
		private function loadData():void
		{
			var request:URLRequest = new URLRequest(_path);
			_urlLoader = new URLLoader();
			_urlLoader.addEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			_urlLoader.data = null;
			_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlLoader.load(request);
			
		}
		/**
		 * 注意，以下转换，只转换logic、modules包里的内容，其它的不要转换。
		 */
		private function onComplete1(e:Event):void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			
			var content:String = (_urlLoader.data);
			if(!content){
				return;
			}
			_urlLoader.close();
			
			if(_path.indexOf("VerifyAdultPanel") != -1){
				return;
			}
			
			
			var clsArr:Array = _path.split("\\");
			_className = clsArr[clsArr.length - 1];
			
			var jsonObj:Object = JSON.parse(content);
			findText(jsonObj);
			
		}
		private function findText(obj:Object):void
		{
			for(var key:* in obj){
				if(!(obj[key] is String) && !(obj[key] is int)){
					findText(obj[key]);
				}
				else if(key == "label" || key == "text" || key == "labels"){
					var content:String = obj[key];
					var arr:Array = content.match(/[\u4e00-\u9fa5]+/g);
					if(arr.length){//有中文
						
						arr = content.match(/[\u4e00-\u9fa5， ,]+/g);
						var keyStr:String = "";
						var keyIndex:int = 0;
						if(content.indexOf(arr[0]) != 0){
							keyStr += "{0}";
							keyIndex ++;
						}
						for(var j:int=0; j<arr.length; j++){
							if(j == arr.length - 1){
								keyStr += arr[j];
							}
							else{
//								if(arr[j].indexOf(" ") != -1){
								if(arr[j] == " "){
									keyStr += arr[j];
								}
								else{
									keyStr += arr[j] + "{"+keyIndex+"}";
									keyIndex ++;
								}
							}
						}
						if(keyStr == "一"){//
							trace("石头在歌唱。");
						}
						
						LangTranslateUiMan.ins.pushLang(keyStr, _className, _sortIndex++);
					}
				}
			}
		}
		
		
		
		private function onTimer2(e:TimerEvent):void
		{
			var rootFile:File = new File();
			var fnew:File = rootFile.resolvePath(_path2);
			if(fnew.exists){
				
				_timer.removeEventListener(TimerEvent.TIMER, onTimer2);
				_timer.stop();
				_timer = null;
				
				var rootFile:File = new File();
				var fold:File = rootFile.resolvePath(_path);
				
				fnew.moveTo(fold, true);
			}
		}
		
		private function onLoadError1(e:Event):void
		{
			trace("加载是失败");
		}
		private function onLoadError2(e:HTTPStatusEvent):void
		{
			if(e.status >= 400){
				trace("加载是失败2,url="+e.target.url);
			}
		}
		private function onLoadError3(e:Event):void
		{
			trace("安全沙箱错误");
		}
		
		public static var dicIdCount:int = 0;
	}
}